package main

import (
	"github.com/go-xorm/xorm"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/mvc"
	"fmt"
	"time"
)

var x *xorm.Engine

type ProductType struct{
	Id int `xorm:"pk"`
	Name string
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type Product struct{
	Id int `xorm:"pk"`
	Name string 
	Type ProductType `xorm:"-"`
	TypeId int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type ProjectType struct{
	Id int `xorm:"pk"`
	Name string
	CreatedAt time.Time
  	UpdatedAt time.Time 
}

type Project struct{
	Id int `xorm:"pk"`
	Name string
	Type ProjectType `xorm:"-"`
	TypeId int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type Meal struct{
	Id int `xorm:"pk"`
	Name string 
	ProjectList []*Project `xorm:"-"`
	ProductList []*Product `xorm:"-"`
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type Member struct{
	Id int `xorm:"pk"`
	Name string
	Age int 
	Meallist []*Meal `xorm:"-"`
	CreatedAt time.Time
  	UpdatedAt time.Time
}
 
type MealProduct struct{
	Id int `xorm:"pk"`
	MealId int
	ProductId int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type MealProject struct{
	Id int `xorm:"pk"`
	MealId int
	ProjectId int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type MemberMeal struct{
	Id int `xorm:"pk"`
	MemberId int
	MealId int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

func init() {
	var xErr error
	x, xErr = xorm.NewEngine("mysql", "root:new_password@tcp(127.0.0.1:3306)/insert_xorm?charset=utf8")
	if xErr != nil {
		log.Fatal("数据库连接失败:", xErr)
	}
	if err := x.Sync(new(ProductType), new(Product), new(ProjectType), new(Project), new(Meal), new(Member), new(MealProduct), new(MealProject), new(MemberMeal)); err != nil {
		log.Fatal("数据表同步失败:", err)
	}
}

type MemberController struct{
    Ctx  iris.Context
}

func (c *MemberController) PostQuery() mvc.Result { 
    var m []*Member
	x.Find(&m)

	for _, v := range m{
		x.Join("INNER", "member_meal", "member_meal.meal_id = meal.id").Where("member_meal.member_id = ?", v.Id).Find(&v.Meallist)
		fmt.Println(v.Meallist)
		for _, mv := range v.Meallist{
			x.Join("INNER", "meal_project", "meal_project.project_id = project.id").Where("meal_project.meal_id = ?", mv.Id).Find(&mv.ProjectList)
			fmt.Println(mv.ProjectList)
			for _, jv := range mv.ProjectList{
				x.Join("INNER", "project", "project_type.id = project.type_id").Where("project.id = ?",jv.Id).Get(&jv.Type)
				fmt.Println(jv.Type)
			}
			
			x.Join("INNER", "meal_product", "meal_product.product_id = product.id").Where("meal_product.meal_id = ?", mv.Id).Find(&mv.ProductList)
			fmt.Println(mv.ProductList)
			for _, dv := range mv.ProductList{
				x.Join("INNER", "product", "product_type.id = product.type_id").Where("product.id = ?", dv.Id).Get(&dv.Type)
				fmt.Println(dv.Type)
			}	
		}
	}

	return mvc.Response{
        Object : m,
    }
}

func main(){ 
	// m := Member{
	// 	Id : 3,
	// 	Name: "谁与争锋",
	// 	CreatedAt : time.Now(),
  	// 	UpdatedAt :	time.Now(),
	// }
	// x.Insert(&m)
	
	var m Member
	m.Id = 3
	m.Name = "最后一次这样行不行"
	m.Age = 45
	m.UpdatedAt = time.Now()
	x.Id(m.Id).Update(&m)

	// m := Member{
	// 	Name: "血刀老祖",
	// }
	// x.Id(3).Update(&m)

	//m := new(Member)
	// var m Member
	// x.Id(3).Get(&m)
	// fmt.Println(m)
	// m.Age = m.Age + 2
	// x.Id(3).Update(&m)
	// x.Id(3).Get(&m)
	// fmt.Println(m)

	// var ms []Member
	// x.Find(&ms)
	// fmt.Println(ms)

	// m := Member{}
	// x.Id(3).Delete(&m)

	// var ml []*Meal
	// x.Join("INNER", "member_meal", "member_meal.meal_id = meal.id").Where("member_meal.member_id = ?", 2).Find(&ml)
	// fmt.Println(ml)

	// var pt ProductType
	// x.Join("INNER", "product", "product_type.id = product.type_id").Where("product.id = ?", 1).Get(&pt)
	// fmt.Println(pt)

	app := iris.New()

	mvc.New(app).Handle(new(MemberController))
	app.Run(iris.Addr(":0006"))
	
}